import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;

import javax.swing.*;

public class ChangePortFrame extends JFrame{
	JCheckBox box;
	Properties wlasciwosci = PropertyLoader.load("conf/System.properties");
	
	public ChangePortFrame(){
		setTitle("Zmiana portów odbioru transmisji");
		setSize(410,150);
		setLocation(260,0);
		JLabel text = new JLabel("Na jakich portach ma nasłuchiwać aplikacja :");
		final JTextField portVField = new JTextField(4);
		final JTextField portAField = new JTextField(4);
		
		portVField.setText(wlasciwosci.getProperty("PORTV"));
		portAField.setText(wlasciwosci.getProperty("PORTA"));
		portVField.setEnabled(false);
		portAField.setEnabled(false);
		
		JLabel portVLabel = new JLabel("Port video :");
		portVLabel.setLabelFor(portVField);
		JLabel portALabel = new JLabel("Port audio :");
		portALabel.setLabelFor(portAField);
		
		box = new JCheckBox("Urzyj własnych portów");
		box.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				if(box.isSelected()){
					portVField.setEnabled(true);
					portAField.setEnabled(true);
					portVField.setText(wlasciwosci.getProperty("PORTV"));
					portAField.setText(wlasciwosci.getProperty("PORTA"));
				}else{
					portVField.setText("4444");
					portAField.setText("4448");
					portVField.setEnabled(false);
					portAField.setEnabled(false);
					
				}
				
			}
			
		});
		
		JLabel boxLabel = new JLabel("");
		
		JButton button = new JButton("Ustaw");
		JButton cencel = new JButton("Anuluj");
		
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
				String portV = portVField.getText();
				String portA = portAField.getText();
				
				int check_v = Integer.valueOf(portV).intValue();
				int check_a = Integer.valueOf(portA).intValue();
				
				if (check_v%2==1 || check_a%2==1){
					JOptionPane.showMessageDialog(null,"Blad, wprowadz parzyste numery portow ! ");}
					else {
				
				
				
				wlasciwosci.setProperty("PORTV", portV);
				wlasciwosci.setProperty("PORTA", portA);
				
				FileOutputStream os;
				try {
					os = new FileOutputStream("conf/System.properties");
					wlasciwosci.store(os,"Zmiana ustawien");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				dispose();
			}
				}
		});
		
		cencel.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
			
		});
		
		JPanel panelField = new JPanel();
		panelField.add(box);
		panelField.add(boxLabel);
		panelField.add(portVLabel);
		panelField.add(portVField);
		panelField.add(portALabel);
		panelField.add(portAField);
		
		panelField.setLayout(new GridLayout(3,1));
		
		JPanel panelButton = new JPanel();

		panelButton.add(button, BorderLayout.LINE_END);
		panelButton.add(cencel, BorderLayout.AFTER_LINE_ENDS);
		getContentPane().add(panelField, BorderLayout.CENTER);
		getContentPane().add(panelButton, BorderLayout.SOUTH);
		
	}
}
